package com.bilibili.lzx.api;


import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@RestController
public class RESTfulApi {

    private static Map<Integer, Map<String,Object>> restData;
    static{
        restData = new HashMap<Integer, Map<String,Object>>();
        for (int i = 0; i < 3; i++) {
            HashMap<String, Object> data = new HashMap<>();
            data.put("id",i);
            data.put("name","name"+i);
            restData.put(i,data);
        }
    }

    @GetMapping("/rests/{id}")
    public Map<String,Object> getData(int id){
        return restData.get(id);
    }

    @DeleteMapping("/rests/{id}")
    public String deleteData(int id){
        restData.remove(id);
        return "delete success";
    }

    @PostMapping("/rests")
    public String postData(Map<String,Object> data){
        System.out.println(restData.keySet().size());
        Integer[] array = restData.keySet().toArray(new Integer[0]);
        int index = array.length;
        restData.put(index,data);
        return "post success";
    }

    @PutMapping("/rests")
    public String putData(Map<String,Object> data){
        Integer[] array = restData.keySet().toArray(new Integer[0]);
        int index = array.length-1;
        if(restData.containsKey(index)){
            restData.replace(index,data);
        }else{
            restData.put(index+1,data);
        }


        Integer id = Integer.valueOf(data.get("id").toString());
        if(restData.containsKey(index)){
            restData.replace(index,data);
        }else{
            restData.put(index+1,data);
        }
        return "put success";
    }
}
